package com.freeLearning.boot.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freeLearning.boot.course.mapper.CourseReviewMapper;
import com.freeLearning.boot.course.model.VO.CourseReviewPageVO;
import com.freeLearning.boot.course.model.entity.CourseInfo;
import com.freeLearning.boot.course.model.entity.CourseReview;
import com.freeLearning.boot.course.model.query.CourseReviewPageQuery;
import com.freeLearning.boot.course.service.CourseReviewService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class CourseReviewServiceImpl extends ServiceImpl<CourseReviewMapper, CourseReview> implements CourseReviewService {
    private final CourseReviewMapper courseReviewMapper;

    @Override
    public IPage<CourseReview> pageQuery(CourseReviewPageQuery courseReviewPageQuery) {
        // 参数构建
        int pageNum = courseReviewPageQuery.getPageNum();
        int pageSize = courseReviewPageQuery.getPageSize();
        Page<CourseReview> page = new Page<>(pageNum, pageSize);
        return this.courseReviewMapper.selectPage(page, new LambdaUpdateWrapper<CourseReview>()
                .eq(courseReviewPageQuery.getCourseId() != null ,CourseReview::getCourseId, courseReviewPageQuery.getCourseId())
                .eq(courseReviewPageQuery.getUserId() != null ,CourseReview::getUserId, courseReviewPageQuery.getUserId())
                .eq(courseReviewPageQuery.getStatus() != null ,CourseReview::getStatus, courseReviewPageQuery.getStatus())
                .eq(courseReviewPageQuery.getConsultantId() != null ,CourseReview::getConsultantId, courseReviewPageQuery.getConsultantId())
                .like(courseReviewPageQuery.getKeyword() != null && !courseReviewPageQuery.getKeyword().equals(""), CourseReview::getComment, courseReviewPageQuery.getKeyword())
                .orderByDesc(CourseReview::getReviewTime)
        );
    }

    @Override
    public IPage<CourseReviewPageVO> queryPageByCourseId(
            Integer pageNum,
            Integer pageSize,
            Long courseId,
            Long userId,
            String sortKey
    ) {
        Page<CourseReviewPageVO> page = new Page<>(pageNum, pageSize);
        return courseReviewMapper.queryPageByCourseId(page, courseId, userId,sortKey);
    }

    @Override
    public IPage<CourseReviewPageVO> queryPageByCourseIdNoUserId(Integer pageNum, Integer pageSize, Long courseId,Long userId,String sortKey) {
        Page<CourseReviewPageVO> page = new Page<>(pageNum, pageSize);
        return courseReviewMapper.queryPageByCourseIdNoUserId(page, courseId,userId,sortKey);
    }
}
